<template>
	<view class="index">
		<image src="../../static/indexBg.png" mode="" class="bg"></image>
		<view class="top">
			<view class="loginText">{{nickname? '欢迎'+nickname:"欢迎登录"}}</view>
			<view class="subtitle">爸爸的果园</view>
			<view class="df top-bottom">
				<view v-for="(v,i) in detailList" :key="i">
					<view class="img">
						<image class="img" :src="v.src"></image>
					</view>
					<view class="text">
						{{v.text}}
						<span>{{v.unit}}</span>
					</view>
				</view>
			</view>
		</view>
		<view class="bottom">
			<view class="title df">
				<view class="text">我的农场</view>
				<view class="look" @click="jumpMyFarm('/pages/user/farm')">查看></view>
			</view>
			<view class="farm" v-if="adoptionList && adoptionList.length">
				<swiper style="height: 280rpx;" @change="swiperChange">
					<swiper-item v-for="(item,index) in adoptionList" :key="index" @click="jumpMyFarm('/pages/user/farm')">
						<image class="farmBg" :src="item.plantPicUrl" mode="aspectFill"></image>
						<view class="df detail">
							<view class="left df" style="white-space: nowrap;">
								<image src="../../static/area.png" mode=""></image>
								<view class="text">
									{{item.plantName}}
								</view>
							</view>
							<view class="right">
								<image src="../../static/rightIcon.png" mode="" @click="jumpMyFarm('/pages/user/farm')"></image>
							</view>
						</view>
					</swiper-item>
				</swiper>
			</view>
			<view v-else>
				<u--text  bold align="center" type="info" text="抱歉,您还没有认养果树"></u--text>
			</view>
			<view class="title df">
				<view class="text">认养列表</view>
			</view>
			<view class="list" v-if="adoptionList && !adoptionList.length == 0">
				<view v-for="(v,i) in adoptionList" :key="i" class="vfor">
					<view class="df tt">
						<view class="title">{{v.plantName}}</view>
						<view class="tag">{{v.state == ( 0 || null) ? "待种植":v.state == 1 ? "待采收":"已采收"}}</view>
					</view>
					<view class="df dd">
						<view class="img">
							<image class="img" :src="v.plantPicUrl"></image>
						</view>
						<view class="detail">
							<view class="com df">
								<view class="detail-title">认养编号:</view>
								<view class="text">{{v.fosterId}}</view>
							</view>
							<view class="com df">
								<view class="detail-title">认养时间:</view>
								<view class="text">{{v.fosterTime || '暂无数据'}}</view>
							</view>
							<view class="com df">
								<view class="detail-title">状态:</view>
								<view class="text">{{v.state == ( 0 || null) ? "待种植":v.state == 1 ? "待采收":"已采收"}}</view>
							</view>
						</view>
					</view>
					<view class="status">
						<scroll-view :scroll-x="true" class="scrollView">
							<view class="scroll-box" v-if="v.taskTypeS ? v.taskTypeS.length:false">
								<u-steps :current="v.taskTypeS? v.taskTypeS.length-1:''" activeIcon="checkmark" inactiveIcon="arrow-right">
									<u-steps-item :title="item.type" desc=" " v-for="(item,index) in v.taskTypeS" :key="index">
										<text slot="desc" style="font-size: 20rpx;text-align: center">{{item.time || ''}}</text>
									</u-steps-item>
								</u-steps>
							</view>
							<view style="margin-top: 20rpx;" v-else>
								<u--text  bold align="center" type="info" text="暂时还没有任务信息"></u--text>
							</view>
						</scroll-view>
					</view>
				</view>
			</view>
			<view v-else>
				<u--text  bold align="center" type="info" text="啊哦,没有果树,快去认养一棵吧~"></u--text>
			</view>
		</view>
		<!-- <uni-tabbar></uni-tabbar> -->
	</view>
</template>

<script>
	import { getFosterList } from "@/api/api.js"
	import $api from "@/api/request.js"
	export default {
		data() {
			return {
				detailList: [
					{
						src: require('../../static/coordinate.png'),
						text: '长武县',
						unit: ''
					},
					{
						src: require('../../static/temperature.png'),
						text: '30',
						unit: '℃'
					},
					{
						src: require('../../static/humidity.png'),
						text: '60',
						unit: '%'
					},
					{
						src: require('../../static/speed.png'),
						text: '4',
						unit: '级'
					}
				],
				nickname:'',
				adoptionList: [],
				curItem:{},
			}
		},
		onReady() {
		},
		onLoad() {
			
		},
		onShow() {
			this.getLocationAndWeather();
			//查询
			this.getFosterListData()
			this.getAvatar()
		},
		methods: {
			getAvatar(){
				let ac = uni.getStorageSync("weixinAccount")
				this.nickname = ac.nickName
			},
			swiperChange(e){
				this.curItem = this.adoptionList[e.detail.current]
			},
			jumpMyFarm(){
				console.log(this.curItem);
				uni.navigateTo({
					url:"/pages/user/farm?plantId="+this.curItem.plantId
				})
			},
			getLocationAndWeather(){
				uni.request({
					url:'https://restapi.amap.com/v3/ip?key=9a6a2dd849a6417eea115e31b30c8485',
					success: (res) => {
						if(res && res.data.status == '1'){
							uni.request({
								url:'https://restapi.amap.com/v3/weather/weatherInfo?key=9a6a2dd849a6417eea115e31b30c8485&city='+res.data.adcode,
								success: (res) => {
									// console.log('天气：',res);
									this.detailList[0].text = res.data.lives[0].city
									this.detailList[1].text = res.data.lives[0].temperature
									this.detailList[2].text = res.data.lives[0].humidity
									this.detailList[3].text = res.data.lives[0].windpower
								}
							})
						}else{
							uni.showToast({
								title:"天气查询失败，请刷新",
								icon:"none"
							})
						}
						
					}
				})
			},
			
			async getFosterListData(){
				const result = await getFosterList()
				console.log('res:',result);
				if(result && result.data.code >= 200 && result.data.code < 400){
					this.adoptionList = result.data.data
					this.curItem = this.adoptionList ? this.adoptionList[0] : {}
				}else{
					console.log("请求数据错误");
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.scrollView{
		height: 140rpx;
		white-space: nowrap;
		.scroll-box{
			display: flex;
			flex-wrap: nowrap;
		}
	}
	.index {

		.df {
			display: flex;
		}

		.bg {
			width: 750rpx;
			height: 516rpx;
			position: relative;
			top: 0;
		}

		.top {
			position: absolute;
			top: 144rpx;
			z-index: 2;
			color: #fff;
			// margin-top: 144rpx;
			margin-left: 58rpx;

			.loginText {
				font-size: 54rpx;
				margin-bottom: 16rpx;
				font-weight: bold;
				letter-spacing: 5rpx;
			}

			.subtitle {
				letter-spacing: 5rpx;
				font-size: 26rpx;
				margin-bottom: 54rpx;
			}

			.top-bottom {
				margin-left: -58rpx;
				width: 750rpx;
				justify-content: space-around;

				.img {
					width: 40rpx;
					height: 40rpx;
					margin: auto;
					margin-bottom: 20rpx;
				}

				.text {
					font-size: 32rpx;
					
					span {
						margin-left: 2rpx;
						display: inline-block;
						font-size: 24rpx;
						line-height: -50rpx;
					}
				}
			}
		}

		.bottom {
			position: relative;
			margin-top: -85rpx;
			background-color: #F4F6F9;
			border-radius: 36rpx 36rpx 0rpx 0rpx;
			padding: 38rpx 30rpx;

			.title {
				justify-content: space-between;
				margin-bottom: 30rpx;

				.text {
					color: #1D1D1D;
					font-size: 32rpx;
					font-weight: bold;
				}

				.look {
					font-size: 28rpx;
					color: #17CC77;
					letter-spacing: 4rpx;
				}
			}

			.farm {
				position: relative;
				margin-bottom: 54rpx;
				
				.farmBg {
					width: 100%;
					height: 100%;
					border-radius: 20rpx;
				}

				.detail {
					position: absolute;
					bottom: 16rpx;
					left: 16rpx;
					width: 658rpx;
					justify-content: space-between;

					.left {
						width: 278rpx;
						height: 62rpx;
						background: rgba(255, 255, 255, 0.6);
						border-radius: 32rpx;
						box-sizing: border-box;
						padding: 14rpx 16rpx;

						image {
							width: 38rpx;
							height: 38rpx;
							margin-right: 12rpx;
						}

						.text {
							font-size: 26rpx;
							color: #1D1D1D;
						}
					}

					.right {
						width: 62rpx;
						height: 62rpx;
						background: rgba(255, 255, 255, 0.6);
						border-radius: 50%;
						box-sizing: border-box;
						padding: 15rpx;

						image {
							width: 32rpx;
							height: 32rpx;
						}
					}
				}
			}

			.list {
				margin-bottom: 20rpx;

				.vfor {
					width: 690rpx;
					// height: 466rpx;
					background: #FFFFFF;
					box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.04);
					box-sizing: border-box;
					padding: 20rpx;
					margin-bottom: 20rpx;
					.tt {
						justify-content: space-between;
						margin-bottom: 0rpx;

						.title {
							font-size: 30rpx;
							font-weight: bold;
							color: #282828;
							line-height: 60rpx;
							letter-spacing: 4rpx;
						}

						.tag {
							width: 120rpx;
							height: 60rpx;
							background: rgba(0, 122, 254, 0.1);
							border-radius: 8rpx;
							border: 1rpx solid #037AFF;
							color: #007AFF;
							font-size: 28rpx;
							text-align: center;
							line-height: 60rpx;
						}
					}

					.dd {
						margin-bottom: 29rpx;

						.img {
							width: 289rpx;
							height: 200rpx;
							border-radius: 10rpx;
							margin-right: 26rpx;
						}

						.detail {
							.com {
								margin-top: 20rpx;
								color: #1D1D1D;
								font-size: 26rpx;

								.detail-title {
									color: rgba(68, 78, 104, 0.7);
									width: 130rpx;
									white-space: nowrap;
									text-align: justify;
									text-align-last: justify;
									margin-right: 20rpx;
								}

								.text {
									width: 172rpx;
								}
							}
						}
					}

					.h1 {
						width: 650rpx;
						height: 1rpx;
						background: rgba(0, 0, 0, 0.12);
						margin-bottom: 8rpx;
					}
				}
			}
		}
	}
</style>